package com.ruoyi.gqgl.psb.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gqgl.psb.domain.Psb;
import com.ruoyi.gqgl.psb.service.IPsbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * PSController
 *
 * @author huacai
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/gqgl/psb")
public class PsbController extends BaseController {
    @Resource
    private IPsbService psbService;

    /**
     * 查询PS列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Psb psb) {
        startPage();
        List<Psb> list = psbService.selectPsbList(psb);
        return getDataTable(list);
    }

    /**
     * 导出PS列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:export')")
    @Log(title = "PS", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Psb psb) {
        List<Psb> list = psbService.selectPsbList(psb);
        ExcelUtil<Psb> util = new ExcelUtil<Psb>(Psb.class);
        util.exportExcel(response, list, "PS数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Psb> util = new ExcelUtil<Psb>(Psb.class);
        util.importTemplateExcel(response, "PS数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "PS", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('gqgl:psb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Psb> util = new ExcelUtil<Psb>(Psb.class);
        InputStream inputStream = file.getInputStream();
        List<Psb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = psbService.batchInsertPsb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取PS详细信息
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:query')")
    @GetMapping(value = "/{psbId}")
    public AjaxResult getInfo(@PathVariable("psbId") String psbId) {
        return success(psbService.selectPsbByPsbId(psbId));
    }

    /**
     * 新增PS
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:add')")
    @Log(title = "PS", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Psb psb) {
        return toAjax(psbService.insertPsb(psb));
    }

    /**
     * 修改PS
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:edit')")
    @Log(title = "PS", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Psb psb) {
        return toAjax(psbService.updatePsb(psb));
    }

    /**
     * 删除PS
     */
    @PreAuthorize("@ss.hasPermi('gqgl:psb:remove')")
    @Log(title = "PS", businessType = BusinessType.DELETE)
    @DeleteMapping("/{psbIds}")
    public AjaxResult remove(@PathVariable String[] psbIds) {
        return toAjax(psbService.deletePsbByPsbIds(psbIds));
    }

    /**
     * 根据认定年度不分页查询PS表
     */
    @GetMapping("/selectPSbByRdNd/{rdNd}")
    public AjaxResult selectPSbByRdNd(@PathVariable("rdNd") String rdNd) {
        List<Psb> list = psbService.selectPSbByRdNd(rdNd);
        return AjaxResult.success(list);
    }

}
